﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace SpaceArcade
{
    class SaveResultForm : Form
    {
        private TextBox _name;
        private Button Save;
        private Button CloseButton;
        private CheckBox RestartCheck;
        private int _score;

        public SaveResultForm(int Score)
        {
            this.Width = 250;
            this.Height = 200;

            List<int> scores = new List<int>();
            bool IsRecord = false;
            string tempstr;           
            System.IO.FileInfo fl = new System.IO.FileInfo(GamePath.gamePath + "records.txt");
            if (fl.Exists)
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(GamePath.gamePath + "records.txt"))
                 {                     
                         while ((tempstr = sr.ReadLine()) != null)
                         { 
                             int temp = Convert.ToInt32(tempstr.Substring(tempstr.IndexOf(':') + 1));
                             scores.Add(temp);                                                     
                         }                     
                 }
            }else if (!fl.Exists)
            {
                IsRecord = true;
                MessageBox.Show("oi");
            }       

            
            if (Score > scores.Max())
                IsRecord = true;
            

            Label TextLabel = new Label();
            TextLabel.Location = new Point(25, 10);
            TextLabel.Font = new Font("Arial", 11, FontStyle.Bold);
            TextLabel.ForeColor = Color.Green;
            TextLabel.Height = 100;
            TextLabel.Width = 200;
            if (IsRecord)
                TextLabel.Text = string.Format("Congratulation! You have reached a new record ! Your score is {0}.", Score);
            else
                TextLabel.Text =string.Format("Game Over! Your score is {0}. Do you want to save your score ?", Score);

            _name = new TextBox();
            _name.Width = 200;
            _name.Location = new Point(25, 75);
            _name.Text = "Enter your name here";
            _name.Click += ClearTextbox;

            CloseButton = new Button();
            CloseButton.Location = new Point(150, 125);
            CloseButton.Text = "Cancel";
            CloseButton.Click += Close;
           
            Save = new Button();
            Save.Location = new Point(25, 125);
            Save.Text = "Save";
            Save.Parent = this;
            Save.Click += SaveToFile;

            RestartCheck = new CheckBox();
            RestartCheck.Location = new Point(25, 100);
            RestartCheck.Text = "Restart game?";
          
            
            this.Controls.Add(_name);
            this.Controls.Add(Save);
            this.Controls.Add(CloseButton);
            this.Controls.Add(RestartCheck);
            this.Controls.Add(TextLabel);

            this._score = Score;
        }

        public void SaveToFile(object sender, EventArgs e)
        {
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(GamePath.gamePath + "records.txt", true))
           {
               if (_name.Text != "")
               {
                   sw.WriteLine(_name.Text + ":" + _score);
               }else
               {
                   MessageBox.Show("Enter your name!");
               }
           }

           if (RestartCheck.Checked)
           {
               this.Close();
               Application.Restart();
           }else
           {
               this.Close();
               Application.Exit();
           }
              
        }

        public void Close(object sender, EventArgs e)
        {
            if (RestartCheck.Checked)
            {
                this.Close();
                Application.Restart();
            }
            else
            {
                this.Close();
                Application.Exit();
            }
        }

        public void ClearTextbox(object sender, EventArgs e)
        {
            _name.Clear();
        }

        
    }
}
